package com.example.zhaojianxu.signin.car.mycar

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.bean.BaseActivity
import com.example.zhaojianxu.signin.car.http.MyCarEntity
import com.example.zhaojianxu.signin.common.constance.ComConstance
import com.example.zhaojianxu.signin.common.http.HttpUtil
import com.example.zhaojianxu.signin.common.util.ToastUtil
import de.greenrobot.event.EventBus
import kotlinx.android.synthetic.main.activity_my_car.*
import kotlinx.android.synthetic.main.common_title_bar.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MyCarActivity : BaseActivity() {

    var mAdapter: MyCarAdapter? = null

    override fun initView() {
        common_back.setOnClickListener {
            finish()
        }
        common_setting.visibility = View.GONE
        common_title.text = "我的租车"


        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        activity_my_car_2.layoutManager = layoutManager

        mAdapter = MyCarAdapter()
        activity_my_car_2.adapter = mAdapter

        activity_my_car_swipe.setOnRefreshListener { initData() }
    }

    override fun initData() {
        HttpUtil.instance.getCarApi()?.postQueryMyCar(ComConstance.UID)?.enqueue(object: Callback<MutableList<MyCarEntity>>{
            override fun onFailure(call: Call<MutableList<MyCarEntity>>, t: Throwable) {
            }

            override fun onResponse(call: Call<MutableList<MyCarEntity>>, response: Response<MutableList<MyCarEntity>>) {
                if (response.isSuccessful && response.body() != null){

                    mAdapter?.mList = response.body()!!
                    mAdapter?.notifyDataSetChanged()
                    activity_my_car_swipe.isRefreshing = false
                }
            }

        })


    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my_car)
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }


    fun onEvent(event: MyCarEvent){
        ToastUtil.instance.showToast(this,"租赁车辆已还！！！")
        initData()
    }
}
